
reg_coeff=[];
rsquared=[];
cnt=0;

for exp_id=1:33
    
    if AV_data(exp_id).ACode==1 & AV_data(exp_id).layer==2
        rt=AV_data(exp_id).velP_ind(round(AV_data(exp_id).frame_times/10));
        ft=AV_data(exp_id).velM_ind(round(AV_data(exp_id).frame_times/10));
        
        rt=AV_data(exp_id).velP(round(AV_data(exp_id).frame_times))';
        ft=AV_data(exp_id).velM(round(AV_data(exp_id).frame_times))';
        
        
        for ind=1:size(AV_data(exp_id).raw_act,2)
            cnt=cnt+1;
            act=AV_data(exp_id).raw_act(:,ind);
            act(act<1.2)=1;
            tmp=regress(act,[ones(length(ft),1) ft]);
            reg_coeff(1,cnt)=tmp(2);
            tmp=regress(act,[ones(length(ft),1) rt]);
            reg_coeff(2,cnt)=tmp(2);
            tmp=regress(act,[ones(length(ft),1) rt&ft]);
            reg_coeff(3,cnt)=tmp(2);
            tmp=regress(act,[ones(length(ft),1) rt&~ft]);
            reg_coeff(4,cnt)=tmp(2);
            tmp=regress(act,[ones(length(ft),1) ~rt&ft]);
            reg_coeff(5,cnt)=tmp(2);
            
            tmp=corrcoef([act ft rt rt&ft rt&~ft ~rt&ft]);
            rsquared(:,cnt)=tmp(1,2:6).^2;
            
        end
    end
end


reg_coeff(reg_coeff>10000)=0;
reg_coeff(reg_coeff<-10000)=0;


% rcc=[];
% rcc(:,1)=hist(reg_coeff(1,:),[-2:0.02:2]);
% rcc(:,2)=hist(reg_coeff(2,:),[-2:0.02:2]);
% rcc(:,3)=hist(reg_coeff(3,:),[-2:0.02:2]);
% rcc(:,4)=hist(reg_coeff(4,:),[-2:0.02:2]);
% rcc(:,5)=hist(reg_coeff(5,:),[-2:0.02:2]);
% figure;
% plot(rcc,'linewidth',2)
% legend({'ft' 'rt' 'rt&ft' 'rt&~ft' '~rt&ft'})

figure;hold on
plot(reg_coeff(1,:),reg_coeff(2,:),'.','markersize',10);
xlim([-0.4 1])
ylim([-0.4 1])
plot([-1 1],[-1 1],'k')
plot([-1 1],[0 0],'k')
plot([0 0],[-1 1],'k')
xlabel('Correlation between activity and visual flow')
ylabel('Correlation between activity and running')
set(gcf,'position',[-1000 300 500 480])
% figure;hold on
% plot(reg_coeff(4,:),reg_coeff(5,:),'.');
% xlim([-1 1])
% ylim([-1 1])
% plot([-1 1],[-1 1],'k')
% plot([-1 1],[0 0],'k')
% plot([0 0],[-1 1],'k')

rsc=[];
rsc(:,1)=hist(rsquared(1,:),[0:0.02:0.5]);
rsc(:,2)=hist(rsquared(2,:),[0:0.02:0.5]);
%rsc(:,3)=hist(rsquared(3,:),[0:0.01:0.5]);
% rsc(:,4)=hist(rsquared(4,:),[0:0.01:0.5]);
%rsc(:,5)=hist(rsquared(5,:),[0:0.01:0.5]);
expfit=[];
for ind=1:2
    tmp=log(log(rsc(:,ind)))';
    infind=find(abs(tmp)>1e6,1,'first');
    expfit(:,ind)=exp(polyval(polyfit([1:infind-1],tmp(1:infind-1),1),[1:1:26]));
end

figure;
semilogy([0:0.02:0.5],(rsc),'o','linewidth',2.5,'markersize',8);
hold on
clrind='bg';

for ind=1:2
    semilogy([0.0:0.02:0.5],(exp(expfit(:,ind))),'color',clrind(ind),'linewidth',2)
end
legend({'ft' 'rt'})
ylim([1 10000])
box off
set(gcf,'position',[-1000 300 500 480])


clrind='gr';
figure;
hold on
for ind=2:-1:1
    bar([0:0.02:0.5],log10(rsc(:,ind))+1,1,'facecolor',clrind(ind));
    plot([0.0:0.02:0.5],log10(exp(expfit(:,ind)))+1,'color',clrind(ind),'linewidth',2)
end
ylabel('Number of neurons')
xlabel('R^2')
xlim([-0.01 0.43])
ylim([0.7 5])


clrind='gr';
figure;
hold on
for ind=1:2
    plot([0.0:0.02:0.5],cumsum(rsc(:,ind))'/sum(rsc(:,1)),'color',clrind(ind),'linewidth',3)
end
xlim([0 0.4])
ylim([0.6 1.01])
ylabel('Number of neurons')
xlabel('R^2')










